
#include "aky_program/aky_com_include.h"
static void my_event_cb(lv_obj_t * obj, lv_event_t event);
void sw_set_stat(lv_obj_t * obj_sw, uint8_t flag_on_off);
static void switch_show_stat(uint8_t stat);
static void switch_click_stat(uint8_t stat);
void adjust_stat_pro(void);


static lv_obj_t *label_back;          //
static lv_obj_t *label_turn_on_off;          //

static lv_obj_t *label_left_title;          //
static lv_obj_t *label_right_title;          //
static lv_obj_t *sw_left_on_off;          //
static lv_obj_t *sw_right_on_off;          //

static lv_obj_t *label_left_pres_set;          //
static lv_obj_t *label_right_pres_set;          //
static lv_obj_t *roller_left_pres_set;          //
static lv_obj_t *roller_right_pres_set;          //
static lv_obj_t *img_left_pres_up;          //
static lv_obj_t *img_right_pres_up;          //
static lv_obj_t *img_left_pres_down;          //
static lv_obj_t *img_right_pres_down;          //
static lv_obj_t *label_left_time_set;          //
static lv_obj_t *label_right_time_set;          //
static lv_obj_t *roller_left_time_set;          //
static lv_obj_t *roller_right_time_set;          //

static lv_obj_t *label_left_time;          //间隔时间
static lv_obj_t *label_right_time;          //

static lv_obj_t *label_left_real_time;          //倒计时时间
static lv_obj_t *label_right_real_time;          //

static lv_obj_t *label_2_arm_syn;          //双臂同步
static lv_obj_t *sw_2_arm_syn;          //双臂同步

static lv_obj_t *img_left_time_up;          //
static lv_obj_t *img_right_time_up;          //
static lv_obj_t *img_left_time_down;          //
static lv_obj_t *img_right_time_down;          //


uint8_t flag_left_arm_on_off = 1;
uint8_t flag_right_arm_on_off = 1;
uint8_t flag_2_arm_syn = 0;

uint16_t left_pres_set_val = 0;
uint16_t left_time_set_val = 0;
uint16_t left_start_mode = 0;

uint16_t right_pres_set_val = 0;
uint16_t right_time_set_val = 0;

uint16_t right_start_mode = 0;
uint16_t right_real_time;

typedef enum
{
    ADJUST_IDLE = 0,
    ADJUST_START,
    ADJUST_ERR,

    ADJUST_UNDEF,
}e_adjust_stat_t;


e_adjust_stat_t e_adjust_stat = ADJUST_IDLE;
e_adjust_stat_t e_adjust_stat_last = ADJUST_UNDEF;

/*
 * 界面回调函数
 */
static void screen014_on_timer(void *user_data)
{
    /* 界面定时器回调函数 */
    off_time_clr();
    adjust_stat_pro();

}

static void screen014_start(synwit_id_t screen_id)
{
    /* 在界面被显示给用户前，这个接口会被调用。 */
    flag_left_arm_on_off = 1;
    flag_right_arm_on_off = 1;
    flag_2_arm_syn = 0;
    left_pres_set_val = 0;
    left_time_set_val = 0;
    right_pres_set_val = 0;
    right_time_set_val = 0;
    left_start_mode = 0;
    right_start_mode = 0;

    label_back = synwit_ui_find_lv_obj(WIDGET_LABEL70);
    label_turn_on_off = synwit_ui_find_lv_obj(WIDGET_LABEL60);
    label_left_title = synwit_ui_find_lv_obj(WIDGET_LABEL63);
    label_right_title = synwit_ui_find_lv_obj(WIDGET_LABEL64);
    label_left_pres_set = synwit_ui_find_lv_obj(WIDGET_LABEL67);
    label_right_pres_set = synwit_ui_find_lv_obj(WIDGET_LABEL66);
    sw_left_on_off = synwit_ui_find_lv_obj(WIDGET_SW3);
    sw_right_on_off = synwit_ui_find_lv_obj(WIDGET_SW4);
    roller_left_pres_set = synwit_ui_find_lv_obj(WIDGET_ROLLER_2);
    roller_right_pres_set = synwit_ui_find_lv_obj(WIDGET_ROLLER_1);
    img_left_pres_up = synwit_ui_find_lv_obj(WIDGET_IMAGE39);
    img_right_pres_up = synwit_ui_find_lv_obj(WIDGET_IMAGE43);
    img_left_pres_down = synwit_ui_find_lv_obj(WIDGET_IMAGE40);
    img_right_pres_down = synwit_ui_find_lv_obj(WIDGET_IMAGE44);
    label_left_time_set = synwit_ui_find_lv_obj(WIDGET_LABEL68);
    label_right_time_set = synwit_ui_find_lv_obj(WIDGET_LABEL69);
    roller_left_time_set = synwit_ui_find_lv_obj(WIDGET_ROLLER_3);
    roller_right_time_set = synwit_ui_find_lv_obj(WIDGET_ROLLER_4);

    label_left_time = synwit_ui_find_lv_obj(WIDGET_LABEL82);
    label_right_time = synwit_ui_find_lv_obj(WIDGET_LABEL81);

    label_2_arm_syn = synwit_ui_find_lv_obj(WIDGET_LABEL65);
    sw_2_arm_syn = synwit_ui_find_lv_obj(WIDGET_SW5);
    img_left_time_up = synwit_ui_find_lv_obj(WIDGET_IMAGE41);
    img_right_time_up = synwit_ui_find_lv_obj(WIDGET_IMAGE45);
    img_left_time_down = synwit_ui_find_lv_obj(WIDGET_IMAGE42);
    img_right_time_down = synwit_ui_find_lv_obj(WIDGET_IMAGE46);
    label_left_real_time = synwit_ui_find_lv_obj(WIDGET_LABEL83);
    label_right_real_time = synwit_ui_find_lv_obj(WIDGET_LABEL84);

    sw_set_stat(sw_left_on_off, flag_left_arm_on_off);
    sw_set_stat(sw_right_on_off, flag_right_arm_on_off);
    sw_set_stat(sw_2_arm_syn, flag_2_arm_syn);

    switch_show_stat(0);
    switch_click_stat(0);

    lv_obj_set_click(label_back, true);
    lv_obj_set_click(label_turn_on_off, true);
    lv_obj_set_click(img_left_pres_up, true);
    lv_obj_set_click(img_left_pres_down, true);
    lv_obj_set_click(img_left_time_up, true);
    lv_obj_set_click(img_left_time_down, true);

    lv_obj_set_click(img_right_pres_up, true);
    lv_obj_set_click(img_right_pres_down, true);
    lv_obj_set_click(img_right_time_up, true);
    lv_obj_set_click(img_right_time_down, true);


    lv_obj_set_event_cb(label_back, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(label_turn_on_off, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(sw_2_arm_syn, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(sw_left_on_off, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(sw_right_on_off, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(roller_left_pres_set, my_event_cb);   /*Assign an event callback*/

    lv_obj_set_event_cb(img_left_pres_up, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(img_left_pres_down, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(img_left_time_up, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(img_left_time_down, my_event_cb);   /*Assign an event callback*/

    lv_obj_set_event_cb(img_right_pres_up, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(img_right_pres_down, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(img_right_time_up, my_event_cb);   /*Assign an event callback*/
    lv_obj_set_event_cb(img_right_time_down, my_event_cb);   /*Assign an event callback*/
    // 打开下面的注释可以为本界面开启一个每100ms触发一次的定时器
    synwit_ui_start_scr_timer(10, NULL);
}

static void screen014_stop(synwit_id_t screen_id)
{
    /* 准备切换到其它界面前，这个接口会被调用。 */
}

static int screen014_favorite_stop_policy()
{
    /* 函数返回值表示本界面结束时，希望采用的资源管理策略 */

    /* 资源管理策略(主要影响本界面再次进入时的加载速度):
     *    0: 系统推荐，释放当前界面引用的资源，保留控件及控件状态。可获得平衡的界面切换性能和内存消耗
     *    1: 释放当前界面引用的资源，并删除所有控件。
     *    2: 保留当前界面的所有资源及控件。可以获得最佳的界面切换性能，但内存消耗较大
     */
    return 0;
}


/*
 * 界面注册对象
 */
synwit_screen_callback_t screen014_cb_obj = {
    .favorite_stop_policy = screen014_favorite_stop_policy,
    .on_screen_start = screen014_start,
    .on_screen_stop = screen014_stop,
    .on_screen_timer = screen014_on_timer,
};

static void my_event_cb(lv_obj_t * obj, lv_event_t event)
{
    uint8_t stat = 0;
    if (e_adjust_stat == ADJUST_IDLE)
    {
        if (obj == label_back)
        {
            if (event == LV_EVENT_CLICKED)
            {
                synwit_ui_load_screen(SCREEN012);
            }
        }
        if (obj == label_turn_on_off)
        {
            if (event == LV_EVENT_CLICKED)
            {
                if(flag_2_arm_syn == 1 || (flag_left_arm_on_off == 1 || flag_right_arm_on_off == 1))
                {
                    e_adjust_stat = ADJUST_START;
                }
            }
        }
        if (obj == sw_2_arm_syn)
        {
            if (event == LV_EVENT_VALUE_CHANGED)
            {
                if (flag_left_arm_on_off == 1 && flag_right_arm_on_off == 1)
                {
                    stat = lv_switch_get_state(sw_2_arm_syn);
                    flag_2_arm_syn = stat;
                    if (flag_2_arm_syn == 0)
                    {
                        switch_show_stat(0);
                        switch_click_stat(0);
                    }
                    else
                    {
                        switch_show_stat(1);
                        switch_click_stat(2);
                    }
                }
            }
        }
        if (obj == sw_left_on_off)
        {
            if (event == LV_EVENT_VALUE_CHANGED)
            {
                stat = lv_switch_get_state(sw_left_on_off);
                flag_left_arm_on_off = stat;
                if (flag_left_arm_on_off == 0)
                {
                    switch_click_stat(1);
                }
                else if (flag_right_arm_on_off == 1 && flag_left_arm_on_off == 1)
                {
                    switch_click_stat(0);
                }
    
            }
        }
        if (obj == sw_right_on_off)
        {
            if (event == LV_EVENT_VALUE_CHANGED)
            {
                stat = lv_switch_get_state(sw_right_on_off);
                flag_right_arm_on_off = stat;
                if (flag_right_arm_on_off == 0)
                {
                    switch_click_stat(1);
                }
                else if (flag_right_arm_on_off == 1 && flag_left_arm_on_off == 1)
                {
                    switch_click_stat(0);
                }
            }
        }
        if (obj == roller_left_pres_set)
        {
            if (event == LV_EVENT_VALUE_CHANGED)
            {
                stat = lv_roller_get_selected(roller_left_pres_set);
                printf("roller_left_pres_set stat :%d\r\n",stat);
    
            }
        }
    

    }
    else if (e_adjust_stat == ADJUST_START)
    {
        if (obj == label_turn_on_off)
        {
            if (event == LV_EVENT_CLICKED)
            {
                e_adjust_stat = ADJUST_IDLE;
                printf("label_turn_on_off LV_EVENT_CLICKED\n");
                aky_task_0_send_cmd(TASK0_BP_STOP_Command);
                aky_task_1_send_cmd(TASK1_BP_STOP_Command);
                aky_block_delay_ms(150);
                aky_task_0_send_cmd(TASK0_BP_STOP_Command);
                aky_task_1_send_cmd(TASK1_BP_STOP_Command);
                aky_block_delay_ms(150);

                aky_task_0_send_cmd(TASK0_BP_STOP_Command);
                aky_task_1_send_cmd(TASK1_BP_STOP_Command);
            
            }
        }

//左侧调节------------------------------
        if (obj == img_left_pres_up)
        {
            if (event == LV_EVENT_CLICKED)
            {
                left_pres_set_val+=10;
                if (left_pres_set_val > 260)
                {
                    left_pres_set_val = 260;
                }
                
                Task0_BP.pre_set = left_pres_set_val;
                aky_task_0_send_cmd(TASK0_PER_UP);
                lv_label_set_text_fmt(label_left_pres_set,"设定压力:%d",left_pres_set_val);
                if (flag_2_arm_syn == 1)
                {
                    right_pres_set_val = left_pres_set_val;
                    Task1_BP.pre_set = right_pres_set_val;
                    aky_task_1_send_cmd(TASK1_PER_UP);
                    lv_label_set_text_fmt(label_right_pres_set,"设定压力:%d",right_pres_set_val);
    
                }
                

            }
        }
        if (obj == img_left_pres_down)
        {
            if (event == LV_EVENT_CLICKED)
            {
                left_pres_set_val-=10;
                if (left_pres_set_val < 50)
                {
                    left_pres_set_val = 50;
                }
                Task0_BP.pre_set = left_pres_set_val;
                aky_task_0_send_cmd(TASK0_PER_DOWN);
                lv_label_set_text_fmt(label_left_pres_set,"设定压力:%d",left_pres_set_val);
                if (flag_2_arm_syn == 1)
                {
                    right_pres_set_val = left_pres_set_val;
                    Task1_BP.pre_set = right_pres_set_val;
                    aky_task_1_send_cmd(TASK1_PER_UP);
                    lv_label_set_text_fmt(label_right_pres_set,"设定压力:%d",right_pres_set_val);
    
                }

            }
        }
        if (obj == img_left_time_up)
        {
            if (event == LV_EVENT_CLICKED)
            {
                left_time_set_val+=1;
                if (left_time_set_val > 10)
                {
                    left_time_set_val = 10;
                }
                
                Task0_BP.pre_hold_time = left_time_set_val;
                aky_task_0_send_cmd(TASK0_TIME_ADJUST);
                lv_label_set_text_fmt(label_left_time,"间隔时间:%d分钟",left_time_set_val);
                if (flag_2_arm_syn == 1)
                {
                    right_time_set_val = left_time_set_val;
                    Task1_BP.pre_hold_time = right_time_set_val;
                    aky_task_1_send_cmd(TASK1_TIME_ADJUST);
                    lv_label_set_text_fmt(label_right_time,"间隔时间:%d分钟",right_time_set_val);
        
                }

            }
        }

        if (obj == img_left_time_down)
        {
            if (event == LV_EVENT_CLICKED)
            {
                if (left_time_set_val == 1)
                {
                    left_time_set_val = 1;
                }
                else
                {
                    left_time_set_val-=1;
                }
                Task0_BP.pre_hold_time = left_time_set_val;
                aky_task_0_send_cmd(TASK0_TIME_ADJUST);
                lv_label_set_text_fmt(label_left_time,"间隔时间:%d分钟",left_time_set_val);
                if (flag_2_arm_syn == 1)
                {
                    right_time_set_val = left_time_set_val;
                    Task1_BP.pre_hold_time = right_time_set_val;
                    aky_task_1_send_cmd(TASK1_TIME_ADJUST);
                    lv_label_set_text_fmt(label_right_time,"间隔时间:%d分钟",right_time_set_val);
        
                }

            }
        }

        //右侧调节 ----------------------
        if (obj == img_right_pres_up)
        {
            if (event == LV_EVENT_CLICKED)
            {
                right_pres_set_val+=10;
                if (right_pres_set_val > 260)
                {
                    right_pres_set_val = 260;
                }
                
                Task1_BP.pre_set = right_pres_set_val;
                aky_task_1_send_cmd(TASK1_PER_UP);
                lv_label_set_text_fmt(label_right_pres_set,"设定压力:%d",right_pres_set_val);

            }
        }
        if (obj == img_right_pres_down)
        {
            if (event == LV_EVENT_CLICKED)
            {
                right_pres_set_val-=10;
                if (right_pres_set_val < 50)
                {
                    right_pres_set_val = 50;
                }
                Task1_BP.pre_set = right_pres_set_val;
                aky_task_1_send_cmd(TASK1_PER_UP);
                lv_label_set_text_fmt(label_right_pres_set,"设定压力:%d",right_pres_set_val);
 
            }
        }
        if (obj == img_right_time_up)
        {
            if (event == LV_EVENT_CLICKED)
            {
                right_time_set_val+=1;
                if (right_time_set_val > 10)
                {
                    right_time_set_val = 10;
                }

                Task1_BP.pre_hold_time = right_time_set_val;
                aky_task_1_send_cmd(TASK1_TIME_ADJUST);
                lv_label_set_text_fmt(label_right_time,"间隔时间:%d分钟",right_time_set_val);

            }
        }

        if (obj == img_right_time_down)
        {
            if (event == LV_EVENT_CLICKED)
            {
                if (right_time_set_val == 1)
                {
                    right_time_set_val = 1;
                }
                else
                {
                    right_time_set_val-=1;
                }

                Task1_BP.pre_hold_time = right_time_set_val;
                aky_task_1_send_cmd(TASK1_TIME_ADJUST);
                lv_label_set_text_fmt(label_right_time,"间隔时间:%d分钟",right_time_set_val);
            }
        }


    }
}

uint16_t get_pres_by_level(uint8_t level)
{
    uint16_t res = 0;
    if (level == 0)
    {
        res = 180;
    }
    else if (level == 1)
    {
        res = 200;
    }
    else if (level == 2)
    {
        res = 220;
    }
    return res;
}

uint16_t get_mode_by_pres(uint16_t pres)
{
    uint16_t res = 0;
    if (pres == 180)
    {
        res = 1;
    }
    else if (pres == 200)
    {
        res = 3;
    }
    else if (pres == 220)
    {
        res = 4;
    }
    return res;
}

void get_pres_and_time(void)
{
    left_pres_set_val = get_pres_by_level(lv_roller_get_selected(roller_left_pres_set));
    left_time_set_val = lv_roller_get_selected(roller_left_time_set) + 1;
    right_pres_set_val = get_pres_by_level(lv_roller_get_selected(roller_right_pres_set));
    right_time_set_val = lv_roller_get_selected(roller_right_time_set) + 1;
    right_real_time = right_time_set_val * 60;
    left_start_mode = get_mode_by_pres(left_pres_set_val);
    right_start_mode = get_mode_by_pres(right_pres_set_val);

}

static void switch_click_stat(uint8_t stat)
{
    if (stat == 0)
    {
        //默认状态
        lv_obj_set_click(sw_left_on_off,true);
        lv_obj_set_click(sw_right_on_off,true);
        lv_obj_set_click(sw_2_arm_syn,true);
    }
    else if (stat == 1)
    {
        //不可以同步
        lv_obj_set_click(sw_left_on_off,true);
        lv_obj_set_click(sw_right_on_off,true);
        lv_obj_set_click(sw_2_arm_syn,false);
    }
    else if (stat == 2)
    {
        //同步 不可以关闭双臂
        lv_obj_set_click(sw_left_on_off,false);
        lv_obj_set_click(sw_right_on_off,false);
        lv_obj_set_click(sw_2_arm_syn,true);
    }
    
}
static void switch_show_stat(uint8_t stat)
{
    lv_obj_set_hidden(label_back,true);
    lv_obj_set_hidden(sw_left_on_off,true);
    lv_obj_set_hidden(sw_right_on_off,true);
    lv_obj_set_hidden(label_left_pres_set,true);
    lv_obj_set_hidden(label_right_pres_set,true);
    lv_obj_set_hidden(roller_left_pres_set,true);
    lv_obj_set_hidden(roller_right_pres_set,true);
    lv_obj_set_hidden(img_left_pres_up,true);
    lv_obj_set_hidden(img_right_pres_up,true);
    lv_obj_set_hidden(img_left_pres_down,true);
    lv_obj_set_hidden(img_right_pres_down,true);
    lv_obj_set_hidden(label_left_time_set,true);
    lv_obj_set_hidden(label_right_time_set,true);
    lv_obj_set_hidden(roller_left_time_set,true);
    lv_obj_set_hidden(roller_right_time_set,true);
    lv_obj_set_hidden(label_left_time,true);
    lv_obj_set_hidden(label_right_time,true);
    lv_obj_set_hidden(label_2_arm_syn,true);
    lv_obj_set_hidden(sw_2_arm_syn,true);
    lv_obj_set_hidden(img_left_time_up,true);
    lv_obj_set_hidden(img_right_time_up,true);
    lv_obj_set_hidden(img_left_time_down,true);
    lv_obj_set_hidden(img_right_time_down,true);
    lv_obj_set_hidden(label_left_real_time,true);
    lv_obj_set_hidden(label_right_real_time,true);
    lv_obj_set_hidden(label_left_title,true);
    lv_obj_set_hidden(label_right_title,true);
    if (stat == 0)
    {
        //设置 默认状态
        lv_obj_set_hidden(label_back,false);
        lv_obj_set_hidden(sw_left_on_off,false);
        lv_obj_set_hidden(sw_right_on_off,false);
        lv_obj_set_hidden(label_left_pres_set,false);
        lv_obj_set_hidden(label_right_pres_set,false);
        lv_obj_set_hidden(roller_left_pres_set,false);
        lv_obj_set_hidden(roller_right_pres_set,false);

        lv_obj_set_hidden(label_left_time_set,false);
        lv_obj_set_hidden(label_right_time_set,false);
        lv_obj_set_hidden(roller_left_time_set,false);
        lv_obj_set_hidden(roller_right_time_set,false);
        
        lv_obj_set_hidden(label_2_arm_syn,false);
        lv_obj_set_hidden(sw_2_arm_syn,false);
    
        lv_obj_set_hidden(label_left_title,false);
        lv_obj_set_hidden(label_right_title,false);
        
        lv_label_set_text_fmt(label_left_title,"左臂开关");
        lv_label_set_text_fmt(label_right_title,"右臂开关");
        lv_label_set_text_fmt(label_left_pres_set,"压力设定");
        lv_label_set_text_fmt(label_right_pres_set,"压力设定");
        lv_label_set_text_fmt(label_left_time_set,"间隔时间");
        lv_label_set_text_fmt(label_right_time_set,"间隔时间");
        lv_label_set_text_fmt(label_turn_on_off,"启动");

        lv_obj_set_pos(label_turn_on_off,674,527);
        lv_obj_set_pos(label_left_title,60,117);
        lv_obj_set_pos(label_left_pres_set,44,174);
        lv_obj_set_pos(label_left_time_set,51,324);

    }
    else if (stat == 1)
    {
        //设置 同步状态
        lv_obj_set_hidden(label_back,false);
        lv_obj_set_hidden(label_left_pres_set,false);
        lv_obj_set_hidden(roller_left_pres_set,false);
        lv_obj_set_hidden(label_left_time_set,false);
        lv_obj_set_hidden(roller_left_time_set,false);
        lv_obj_set_hidden(label_2_arm_syn,false);
        lv_obj_set_hidden(sw_2_arm_syn,false);

        lv_label_set_text_fmt(label_left_title,"左臂开关");
        lv_label_set_text_fmt(label_right_title,"右臂开关");
        lv_label_set_text_fmt(label_left_pres_set,"压力设定");
        lv_label_set_text_fmt(label_right_pres_set,"压力设定");
        lv_label_set_text_fmt(label_left_time_set,"间隔时间");
        lv_label_set_text_fmt(label_right_time_set,"间隔时间");
        lv_label_set_text_fmt(label_turn_on_off,"启动");
        lv_obj_set_pos(label_turn_on_off,674,527);
        lv_obj_set_pos(label_left_title,60,117);
        lv_obj_set_pos(label_left_pres_set,44,174);
        lv_obj_set_pos(label_left_time_set,51,324);

    }
    else if (stat == 2)
    {
        //开始训练状态 双臂开
        lv_obj_set_hidden(label_left_pres_set,false);
        lv_obj_set_hidden(label_right_pres_set,false);
    
        lv_obj_set_hidden(img_left_pres_up,false);
        lv_obj_set_hidden(img_right_pres_up,false);
        lv_obj_set_hidden(img_left_pres_down,false);
        lv_obj_set_hidden(img_right_pres_down,false);
    
        lv_obj_set_hidden(label_left_time_set,false);
        lv_obj_set_hidden(label_right_time_set,false);
        lv_obj_set_hidden(label_left_time,false);
        lv_obj_set_hidden(label_right_time,false);
            
        lv_obj_set_hidden(img_left_time_up,false);
        lv_obj_set_hidden(img_right_time_up,false);
        lv_obj_set_hidden(img_left_time_down,false);
        lv_obj_set_hidden(img_right_time_down,false);
        lv_obj_set_hidden(label_left_real_time,false);
        lv_obj_set_hidden(label_right_real_time,false);

        lv_label_set_text_fmt(label_left_title,"左臂");
        lv_label_set_text_fmt(label_right_title,"右臂");
        lv_label_set_text_fmt(label_left_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_right_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_left_time_set,"实时压力:");
        lv_label_set_text_fmt(label_right_time_set,"实时压力:");
        lv_label_set_text_fmt(label_left_time,"间隔时间:");
        lv_label_set_text_fmt(label_right_time,"间隔时间:");

        lv_label_set_text_fmt(label_turn_on_off,"停止");
        lv_label_set_text_fmt(label_left_real_time," ");
        lv_label_set_text_fmt(label_right_real_time," ");

        lv_obj_set_pos(label_turn_on_off,400,527);

        lv_obj_set_pos(label_left_title,230,117);
        lv_obj_set_pos(label_left_pres_set,160,174);
        lv_obj_set_pos(label_left_time_set,160,324);

    }
    else if (stat == 3)
    {
        //开始训练状态 同步 不显示右侧 + -
        lv_obj_set_hidden(label_left_pres_set,false);
        lv_obj_set_hidden(label_right_pres_set,false);
        lv_obj_set_hidden(img_left_pres_up,false);
        lv_obj_set_hidden(img_left_pres_down,false);
        lv_obj_set_hidden(label_left_time_set,false);
        lv_obj_set_hidden(label_right_time_set,false);
        lv_obj_set_hidden(label_left_time,false);
        lv_obj_set_hidden(label_right_time,false);
        lv_obj_set_hidden(img_left_time_up,false);
        lv_obj_set_hidden(img_left_time_down,false);
        lv_obj_set_hidden(label_left_real_time,false);

        lv_label_set_text_fmt(label_left_title,"左臂");
        lv_label_set_text_fmt(label_right_title,"右臂");
        lv_label_set_text_fmt(label_left_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_right_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_left_time_set,"实时压力:");
        lv_label_set_text_fmt(label_right_time_set,"实时压力:");
        lv_label_set_text_fmt(label_left_time,"间隔时间:");
        lv_label_set_text_fmt(label_right_time,"间隔时间:");

        lv_label_set_text_fmt(label_turn_on_off,"停止");
        lv_label_set_text_fmt(label_left_real_time," ");
        lv_label_set_text_fmt(label_right_real_time," ");

        lv_obj_set_pos(label_turn_on_off,400,527);

        lv_obj_set_pos(label_left_title,230,117);
        lv_obj_set_pos(label_left_pres_set,160,174);
        lv_obj_set_pos(label_left_time_set,160,324);


    }
    else if (stat == 4)
    {
        //开始训练状态 右侧关闭 左侧打开

        lv_obj_set_hidden(label_left_pres_set,false);
    
        lv_obj_set_hidden(img_left_pres_up,false);
        lv_obj_set_hidden(img_left_pres_down,false);
    
        lv_obj_set_hidden(label_left_time_set,false);
        lv_obj_set_hidden(label_left_time,false);
            
        lv_obj_set_hidden(img_left_time_up,false);
        lv_obj_set_hidden(img_left_time_down,false);
        lv_obj_set_hidden(label_left_real_time,false);

        lv_label_set_text_fmt(label_left_title,"左臂");
        lv_label_set_text_fmt(label_right_title,"右臂");
        lv_label_set_text_fmt(label_left_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_right_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_left_time_set,"实时压力:");
        lv_label_set_text_fmt(label_right_time_set,"实时压力:");
        lv_label_set_text_fmt(label_left_time,"间隔时间:");
        lv_label_set_text_fmt(label_right_time,"间隔时间:");

        lv_label_set_text_fmt(label_turn_on_off,"停止");
        lv_label_set_text_fmt(label_left_real_time," ");
        lv_label_set_text_fmt(label_right_real_time," ");

        lv_obj_set_pos(label_turn_on_off,400,527);

        lv_obj_set_pos(label_left_title,230,117);
        lv_obj_set_pos(label_left_pres_set,160,174);
        lv_obj_set_pos(label_left_time_set,160,324);


    }
    else if (stat == 5)
    {
        //开始训练状态 左侧关闭 右侧打开

        lv_obj_set_hidden(label_right_pres_set,false);
    
        lv_obj_set_hidden(img_right_pres_up,false);
        lv_obj_set_hidden(img_right_pres_down,false);
    
        lv_obj_set_hidden(label_right_time_set,false);
        lv_obj_set_hidden(label_right_time,false);
        
    
        lv_obj_set_hidden(img_right_time_up,false);
        lv_obj_set_hidden(img_right_time_down,false);
        lv_obj_set_hidden(label_right_real_time,false);


        lv_label_set_text_fmt(label_left_title,"左臂");
        lv_label_set_text_fmt(label_right_title,"右臂");
        lv_label_set_text_fmt(label_left_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_right_pres_set,"设定压力:");
        lv_label_set_text_fmt(label_left_time_set,"实时压力:");
        lv_label_set_text_fmt(label_right_time_set,"实时压力:");
        lv_label_set_text_fmt(label_left_time,"间隔时间:");
        lv_label_set_text_fmt(label_right_time,"间隔时间:");

        lv_label_set_text_fmt(label_turn_on_off,"停止");
        lv_label_set_text_fmt(label_left_real_time," ");
        lv_label_set_text_fmt(label_right_real_time," ");

        lv_obj_set_pos(label_turn_on_off,400,527);

        lv_obj_set_pos(label_left_title,230,117);
        lv_obj_set_pos(label_left_pres_set,160,174);
        lv_obj_set_pos(label_left_time_set,160,324);

    }

}

void sw_set_stat(lv_obj_t * obj_sw, uint8_t flag_on_off)
{
    if (flag_on_off == 0)
    {
        lv_switch_off(obj_sw,LV_ANIM_OFF);
    }
    else
    {
        lv_switch_on(obj_sw,LV_ANIM_OFF);
    }
}
static stru_aky_task_0_t s_aky_task_0 = {0};
static stru_aky_task_1_t s_aky_task_1 = {0};

void adjust_stat_pro(void)
{
    static uint16_t step = 0;
    static uint16_t send_cnt = 0;
    static uint32_t u32_tick_last = 0;
    static uint16_t right_syn_step = 0;
    static uint16_t right_step_cnt = 0;
    uint16_t left_real_time;
    static uint8_t sec_last = 0;
    static uint8_t time_syn_step = 0;

    if (e_adjust_stat_last != e_adjust_stat)
    {
        e_adjust_stat_last = e_adjust_stat;
        step = 0;
        u32_tick_last = g_u32_tick_cnt;
    }
    s_aky_task_0 = aky_task_0_get_data();
    s_aky_task_1 = aky_task_1_get_data();

    switch (e_adjust_stat)
    {
    case ADJUST_IDLE:
    {
        if (step == 0)
        {
            if (flag_2_arm_syn == 1)
            {
                switch_show_stat(1);
            }
            else
            {
                switch_show_stat(0);
            }
            step++;
            aky_task_0_send_cmd(TASK0_BP_STOP_Command);
            aky_task_1_send_cmd(TASK1_BP_STOP_Command);

        }
        break;
    }
    case ADJUST_START:
    {
        if (step == 0)
        {
            step++;
            switch_show_stat(2);
            get_pres_and_time();
            clr_task_0_data();
            clr_task_1_data();

            Task0_BP.PER_Mode = left_start_mode;
            Task1_BP.PER_Mode = right_start_mode;            
            Task0_BP.pre_hold_time = left_time_set_val;
            Task1_BP.pre_hold_time = right_time_set_val;
            if(flag_2_arm_syn == 1 || (flag_left_arm_on_off == 1 && flag_right_arm_on_off == 1))
            {
                if (flag_2_arm_syn == 1)
                {
                    right_start_mode = left_start_mode;
                    right_time_set_val = left_time_set_val;
                    Task0_BP.PER_Mode = left_start_mode;
                    Task1_BP.PER_Mode = right_start_mode;            
                    Task0_BP.pre_hold_time = left_time_set_val;
                    Task1_BP.pre_hold_time = right_time_set_val;

                    right_pres_set_val = left_pres_set_val;
                    switch_show_stat(3);
                }
                else
                {
                    Task0_BP.PER_Mode = left_start_mode;
                    Task1_BP.PER_Mode = right_start_mode;            
                    Task0_BP.pre_hold_time = left_time_set_val;
                    Task1_BP.pre_hold_time = right_time_set_val;
                }

                aky_task_0_send_cmd(TASK0_PER_PREPARE_Command);
                aky_task_1_send_cmd(TASK1_PER_PREPARE_Command);
            }
            else if ((flag_left_arm_on_off == 1 && flag_right_arm_on_off == 0))
            {
                aky_task_0_send_cmd(TASK0_PER_PREPARE_Command);
                switch_show_stat(4);

            }
            else if ((flag_left_arm_on_off == 0 && flag_right_arm_on_off == 1))
            {
                aky_task_1_send_cmd(TASK1_PER_PREPARE_Command);
                switch_show_stat(5);
            }

            lv_label_set_text_fmt(label_left_pres_set,"设定压力:%d",left_pres_set_val);
            lv_label_set_text_fmt(label_right_pres_set,"设定压力:%d",right_pres_set_val);
            lv_label_set_text_fmt(label_left_time,"间隔时间:%d分钟",left_time_set_val);
            lv_label_set_text_fmt(label_right_time,"间隔时间:%d分钟",right_time_set_val);
            send_cnt = 0;
            right_syn_step = 0;
            right_step_cnt = 1;
            time_syn_step = 0;
        }
        else if (step == 1)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                
                Task0_BP.PER_Mode = left_start_mode;
                Task1_BP.PER_Mode = right_start_mode;            
                Task0_BP.pre_hold_time = left_time_set_val;
                Task1_BP.pre_hold_time = right_time_set_val;
    
                if(flag_2_arm_syn == 1 || (flag_left_arm_on_off == 1 && flag_right_arm_on_off == 1))
                {
                    if (flag_2_arm_syn == 1)
                    {
                        right_start_mode = left_start_mode;
                        right_time_set_val = left_time_set_val;
                        Task0_BP.PER_Mode = left_start_mode;
                        Task1_BP.PER_Mode = right_start_mode;            
                        Task0_BP.pre_hold_time = left_time_set_val;
                        Task1_BP.pre_hold_time = right_time_set_val;
    
                        right_pres_set_val = left_pres_set_val;
                    }
    
                    aky_task_0_send_cmd(TASK0_PER_PREPARE_Command);
                    aky_task_1_send_cmd(TASK1_PER_PREPARE_Command);
                }
                else if ((flag_left_arm_on_off == 1 && flag_right_arm_on_off == 0))
                {
                    aky_task_0_send_cmd(TASK0_PER_PREPARE_Command);
                }
                else if ((flag_left_arm_on_off == 0 && flag_right_arm_on_off == 1))
                {
                    aky_task_1_send_cmd(TASK1_PER_PREPARE_Command);
                }
                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step = 2; 
                }
                
            }
        }
        else if (step == 2)
        {
            if(flag_2_arm_syn == 1 || (flag_left_arm_on_off == 1 && flag_right_arm_on_off == 1))
            {
                if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_PER_WAIT && s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_PER_WAIT)
                {
                    //等待血压主控归0结束
                    step = 3;
                    aky_task_0_send_cmd(TASK0_PER_START_Command);
                    aky_task_1_send_cmd(TASK1_PER_START_Command);
                    u32_tick_last = g_u32_tick_cnt;
                }
            }
            else if ((flag_left_arm_on_off == 1 && flag_right_arm_on_off == 0))
            {
                if (s_aky_task_0.e_aky_task_0_stat == TASK_0_STAT_PER_WAIT)
                {
                    //等待血压主控归0结束
                    step = 3;
                    aky_task_0_send_cmd(TASK0_PER_START_Command);
                    u32_tick_last = g_u32_tick_cnt;
                }
            }
            else if ((flag_left_arm_on_off == 0 && flag_right_arm_on_off == 1))
            {
                if (s_aky_task_1.e_aky_task_1_stat == TASK_1_STAT_PER_WAIT)
                {
                    //等待血压主控归0结束
                    step = 3;
                    aky_task_1_send_cmd(TASK1_PER_START_Command);
                    u32_tick_last = g_u32_tick_cnt;
                }
            }

        }
        else if (step == 3)
        {
            if (g_u32_tick_cnt - u32_tick_last > 300 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                
                if(flag_2_arm_syn == 1 || (flag_left_arm_on_off == 1 && flag_right_arm_on_off == 1))
                {
                    aky_task_0_send_cmd(TASK0_PER_START_Command);
                    aky_task_1_send_cmd(TASK1_PER_START_Command);
                }
                else if ((flag_left_arm_on_off == 1 && flag_right_arm_on_off == 0))
                {
                    aky_task_0_send_cmd(TASK0_PER_START_Command);
                }
                else if ((flag_left_arm_on_off == 0 && flag_right_arm_on_off == 1))
                {
                    aky_task_1_send_cmd(TASK1_PER_START_Command);
                }

                send_cnt++;
                if (send_cnt >= 3)
                {
                    send_cnt = 0;
                    step = 4; 
                }
                
            }
        }
        else if (step == 4)
        {
            if (g_u32_tick_cnt - u32_tick_last > 1000 / 10)
            {
                u32_tick_last = g_u32_tick_cnt;
                left_real_time = Task0_BP.PER_Steptime;

                if(flag_2_arm_syn == 1 || (flag_left_arm_on_off == 1 && flag_right_arm_on_off == 1))
                {
                    if (flag_2_arm_syn == 1)
                    {
                        if (Task0_BP.flag_step_end == 1)
                        {
                            Task1_BP.step_code = Task0_BP.step_code;
                            aky_task_1_send_cmd(TASK1_BP_NEXT_STEP);
                            send_cnt++;
                            if (send_cnt >= 10)
                            {
                                send_cnt = 0;
                                Task0_BP.flag_step_end = 0;
                            }
                            if (Task1_BP.flag_reply == 1)
                            {
                                Task1_BP.flag_reply = 0;
                                Task0_BP.flag_step_end = 0;
                            }
                        }
                        else
                        {
                            Task1_BP.flag_reply = 0;
                            send_cnt = 0;
                        }        
                    }
                    else if (flag_right_arm_on_off == 1)
                    {
                        if (right_syn_step == 0)
                        {
                            if (right_real_time <= 1)
                            {
                                right_syn_step = 1;
                                right_step_cnt++;
                                send_cnt = 0;
                            }                        
                        }
                        else if(right_syn_step == 1)
                        {
                            Task1_BP.step_code = right_step_cnt;
                            aky_task_1_send_cmd(TASK1_BP_NEXT_STEP);
                            send_cnt++;
                            if (send_cnt >= 10)
                            {
                                send_cnt = 0;
                                right_syn_step = 0;
                                right_real_time = right_time_set_val * 60;
                            }
                            if (Task1_BP.flag_reply == 1)
                            {
                                Task1_BP.flag_reply = 0;
                                right_syn_step = 0;
                                right_real_time = right_time_set_val * 60;
                            }
                        }

                        
                    }

                    lv_label_set_text_fmt(label_left_time_set,"实时压力:%d",Task0_BP.pres_mmg);
                    lv_label_set_text_fmt(label_right_time_set,"实时压力:%d",s_aky_task_1.pres_mmg);
    
                    lv_label_set_text_fmt(label_left_real_time,"%d分钟%d秒",left_real_time/60,left_real_time%60);
                    lv_label_set_text_fmt(label_right_real_time,"%d分钟%d秒",right_real_time/60,right_real_time%60);

                }
                else if ((flag_left_arm_on_off == 1 && flag_right_arm_on_off == 0))
                {
                    lv_label_set_text_fmt(label_left_time_set,"实时压力:%d",Task0_BP.pres_mmg);
    
                    left_real_time = Task0_BP.PER_Steptime;
                    lv_label_set_text_fmt(label_left_real_time,"%d分钟%d秒",left_real_time/60,left_real_time%60);
                }
                else if ((flag_left_arm_on_off == 0 && flag_right_arm_on_off == 1))
                {
                    lv_label_set_text_fmt(label_right_time_set,"实时压力:%d",s_aky_task_1.pres_mmg);

                    if (right_syn_step == 0)
                    {
                        if (right_real_time <= 1)
                        {
                            right_syn_step = 1;
                            right_step_cnt++;
                            send_cnt = 0;
                        }                        
                    }
                    else if(right_syn_step == 1)
                    {
                        Task1_BP.step_code = right_step_cnt;
                        aky_task_1_send_cmd(TASK1_BP_NEXT_STEP);
                        send_cnt++;
                        if (send_cnt >= 10)
                        {
                            send_cnt = 0;
                            right_syn_step = 0;
                            right_real_time = right_time_set_val * 60;
                        }
                        if (Task1_BP.flag_reply == 1)
                        {
                            Task1_BP.flag_reply = 0;
                            right_syn_step = 0;
                            right_real_time = right_time_set_val * 60;
                        }
                    }

                    lv_label_set_text_fmt(label_right_real_time,"%d分钟%d秒",right_real_time/60,right_real_time%60);
                }
    
            }
            if (flag_right_arm_on_off == 1 && flag_2_arm_syn == 0)
            {
                //右臂单独计时
                if (time_syn_step == 0)
                {
                    if (sec_last != g_s_time.secon)
                    {
                        sec_last = g_s_time.secon;
                        if (right_step_cnt % 2 == 1)
                        {
                            //单数为加压阶段
                            if (s_aky_task_1.pres_mmg >= right_pres_set_val)
                            {
                                time_syn_step = 1;
                            }
                        }
                        else
                        {
                            time_syn_step = 1;
                        }
                    }
                }
                else if (time_syn_step == 1)
                {
                    if (sec_last != g_s_time.secon)
                    {
                        sec_last = g_s_time.secon;
                        if (right_real_time > 0)
                        {
                            right_real_time--;
                        }
                        else
                        {
                            time_syn_step = 0;
                        }
                    }
                }
                
            }

        }
        break;
    }

    default:
        break;
    }
    

}
